package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.StudentAppService;
import com.cn.mmd.service.TeacherService;
import com.cn.mmd.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 学生作业
*/
@Controller
@RequestMapping(value = "page/studentWork")
public class StudentWorkController {

    @Autowired
    private WorkService workService;

    @Autowired
    private StudentAppService studentAppService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TokenController tokenController;

    @RequestMapping(value = "/select")
    @ResponseBody public synchronized
    Object StudentWorkSelect(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String,Object> map;
//        String workName = request.getParameter("workName");
        String studentId = request.getParameter("studentId");
        Integer startrow = Integer.parseInt(request.getParameter("startrow"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (startrow<1){
            startrow=1;
        }
        startrow = (startrow - 1) * pageSize;
//        = new Schoolwork(startrow, pageSize, workName);
        StudentWork studentWork;
        if (studentApp != null) {
            Schoolwork schoolwork;
            studentWork = new StudentWork();
            studentWork.setStartrow(startrow);
            studentWork.setPageSize(pageSize);
            studentWork.setStudentid(studentId);
            List<StudentWork> studentWorkList = workService.selectByStuId(studentWork);
//            List<Schoolwork> schoolworkList = workService.selectAlls(schoolwork);
            if (studentWorkList != null&&studentWorkList.size()>0) {
                for (int i = 0; i < studentWorkList.size(); i++) {
                    studentWork = studentWorkList.get(i);
                    schoolwork = workService.selectByPrimaryKey(studentWork.getWorkid());
                    map = new HashMap<String, Object>();
                    if (studentWork != null) {
                        String WorkType = studentWork.getWorktype();
                        if (WorkType.equals("未阅读")) {
                            studentWork = new StudentWork();
                            studentWork.setStudentid(studentId);
                            studentWork.setWorkid(studentWork.getWorkid());
                            studentWork.setWorktype("已阅读");
                            int num = 1 + studentApp.getViewednum();
                            studentApp = new StudentApp();
                            studentApp.setViewednum(num);
                            studentApp.setStudentid(studentId);
                            studentAppService.updateByPrimaryKeySelective(studentApp);
                            TeacherStatistics teacherStatistics = teacherService.selectByPrimaryKey(schoolwork.getTeacherid());
                            if (teacherStatistics!=null) {
                                int num1 = 1 + teacherStatistics.getBeviewed();
                                teacherStatistics = new TeacherStatistics();
                                teacherStatistics.setTeacherid(schoolwork.getTeacherid());
                                teacherStatistics.setBeviewed(num1);
                                teacherService.updateByPrimaryKeySelective(teacherStatistics);
                            }else {
                                teacherStatistics = new TeacherStatistics();
                                teacherStatistics.setTeacherid(schoolwork.getTeacherid());
                                teacherStatistics.setBeviewed(1);
                                teacherService.insertStatistics(teacherStatistics);
                            }
                            workService.updateBy(studentWork);
                        }
                        map.put("SchoolWorkId", schoolwork.getSchoolworkid() + "");
                        map.put("WorkName", schoolwork.getWorkname());
                        map.put("ReleaseTime", schoolwork.getReleasetime());
                        map.put("HandOverTime", schoolwork.getHandovertime());
                        map.put("TeacherId", schoolwork.getTeacherid());
                        map.put("WorkType", studentWork.getWorktype());
                        map.put("schoolWorkContent",schoolwork.getSchoolworkcontent());
                        mapList.add(map);
                    }
                }
                studentWork = new StudentWork();
                studentWork.setStudentid(studentId);
                objectMap.put("total", workService.countLimitNums(studentWork));
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
                objectMap.put("data", mapList);
            } else {
                studentWork = new StudentWork();
                studentWork.setStudentid(studentId);
                objectMap.put("total", workService.countLimitNums(studentWork));
                objectMap.put("code", 201);
                objectMap.put("message", "当前学生没有作业");
            }
        }else {
            studentWork = new StudentWork();
            studentWork.setStudentid(studentId);
            objectMap.put("total", workService.countLimitNums(studentWork));
            objectMap.put("code", 203);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }

    @RequestMapping(value = "/selectOne")
    @ResponseBody public synchronized Object selectOneWork(HttpServletRequest request){
        Map<String,Object> objectMap = new HashMap<String, Object>();
        StudentWork studentWork;
        String studentId = request.getParameter("studentId");
        String teacherId = request.getParameter("TeacherId");
        Integer studentWorkId = Integer.parseInt(request.getParameter("SchoolWorkId"));
        String WorkType = request.getParameter("WorkType");
        String token = request.getParameter("token");
        StudentApp studentApp;
        studentApp = (StudentApp) tokenController.token1(studentId, token);
        if (studentApp != null) {
            if (WorkType.equals("未阅读")) {
                studentWork = new StudentWork();
                studentWork.setStudentid(studentId);
                studentWork.setWorkid(studentWorkId);
                studentWork.setWorktype("未完成");
                    int num = 1 + studentApp.getViewednum();
                    studentApp = new StudentApp();
                    studentApp.setViewednum(num);
                    studentApp.setStudentid(studentId);
                    studentAppService.updateByPrimaryKeySelective(studentApp);
                    TeacherStatistics teacherStatistics = teacherService.selectByPrimaryKey(teacherId);
                    int num1 = 1 + teacherStatistics.getBeviewed();
                    teacherStatistics = new TeacherStatistics();
                    teacherStatistics.setTeacherid(teacherId);
                    teacherStatistics.setBeviewed(num1);
                    teacherService.updateByPrimaryKeySelective(teacherStatistics);
                    workService.updateBy(studentWork);
                    objectMap.put("code", 200);
                    objectMap.put("message", "阅读次数+1");
//                objectMap.put("schoolWorkContent",);
            } else {
                objectMap.put("code", 201);
                objectMap.put("message", "阅读不加次数");
            }
        }else {
            objectMap.put("code", 203);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }


}
